/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jp.co.irep.tech.juten.repository.impl;

import java.util.List;
import java.util.Map;
import jp.co.irep.tech.juten.hibernate.pojos.Aken;
import jp.co.irep.tech.juten.hibernate.utils.HibernateUtil;
import jp.co.irep.tech.juten.repository.AkenRepo;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author toan_nguyen
 */

public class AkenRepoImpl implements AkenRepo{

    private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    @Override
    public List<Aken> selectAken(Map<String, Object> akenMap) {
        Session session = getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Aken.class);
            for (Map.Entry<String, Object> entry : akenMap.entrySet()) {
                if (entry.getValue() != null) {
                    if (entry.getKey().compareTo("id") == 0 && (Short) entry.getValue() > 0) {
                        criteria.add(Restrictions.eq(entry.getKey(), entry.getValue()));
                    }
                    if (entry.getKey().compareTo("data") == 0) {
                        criteria.add(Restrictions.like(entry.getKey(), "%" + entry.getValue() + "%"));
                    }
                }
            }
            List<Aken> akenList = criteria.list();
            session.getTransaction().commit();
            return akenList;

        } catch (HibernateException e) {
            e.getMessage();
            return null;
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}
